package org.snowin.ui;

import org.openqa.selenium.WebDriver;
import org.snowin.utils.GlobalInfo;

import java.io.*;

public class GenerateTheRunningData {
    String sp = GlobalInfo.getFileSeparator() ;
    public void initialDataFile(WebDriver webDriver) {


        File casefile = new File(GlobalInfo.getRootPath() +  this.sp + "tools" + this.sp + "runningData.txt");
        if (casefile.exists()) {
            casefile.delete();
        }
        try
        {
            casefile.createNewFile();
            BufferedWriter fw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(casefile, true), "UTF-8"));
            fw.append(webDriver.toString());
            fw.newLine();
            fw.flush();
            fw.close();


            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(casefile), "UTF-8"));
            String str = null;
            int i = 3;
            boolean isSetToltalNum = false;
            while ((null != (str = reader.readLine())) && (i > 0))
            {
                if ((i == 1) && (str.contains("Total"))) {
                    isSetToltalNum = true;
                }
                i--;
            }
            if (!isSetToltalNum)
            {
                BufferedWriter fw2 = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(casefile, true), "UTF-8"));
                int commonTotalNum = getCaseNumByType("common");
                int interfaceTotalNum = getCaseNumByType("interface");
                int total = commonTotalNum + interfaceTotalNum;
                fw2.append("totalNum=" + total);
                fw2.newLine();
                fw2.append("commonTotalNum=" + commonTotalNum);
                fw2.newLine();
                fw2.append("interfaceTotalNum=" + interfaceTotalNum);
                fw2.newLine();
                fw2.flush();
                fw2.close();
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    private int getCaseNumByType(String type) {
        File caseStoreFile = new File(GlobalInfo.getRootPath() + this.sp + "tools" + this.sp + type + "Case.txt");

        int totalNumber = 0;
        try
        {
            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(caseStoreFile), "UTF-8"));
            String str = "";
            while (null != (str = reader.readLine())) {
                totalNumber++;
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return totalNumber;
    }
}
